12. 练习 — 顺序很重要
顺序很重要
注意,默认情况下,Python 将从上到下按顺序运行每行代码。意味着程序中的代码行顺序可以使程序变化很大。
QUIZ QUESTION: :
下面的代码可以用来绘制蓝色线条,然后绘制黄色线条,如下所示:
你能将代码变成正确的顺序吗?
ANSWER CHOICES:
顺序 |
代码 |
---|---|
|
|
|
|
|
|
|
|
|
|
|
SOLUTION:
顺序 |
代码 |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
再做道练习吧!在下面的 workspace 中,看看你能否画出如下所示的方形,其中每条边的颜色都不一样:
Workspace
This section contains either a workspace (it can be a Jupyter Notebook workspace or an online code editor work space, etc.) and it cannot be automatically downloaded to be generated here. Please access the classroom with your account and manually download the workspace to your local machine. Note that for some courses, Udacity upload the workspace files onto https://github.com/udacity , so you may be able to download them there.
Workspace Information:
- Default file path:
- Workspace type: html-live
- Opened files (when workspace is loaded): n/a
( 备注 :如果你无法打开上面的workspace,请去 这里 )
----
解决方案
import turtle
amy = turtle.Turtle()
amy.color("red")
amy.forward(100)
amy.right(90)
amy.color("orange")
amy.forward(100)
amy.right(90)
amy.color("yellow")
amy.forward(100)
amy.right(90)
amy.color("green")
amy.forward(100)
同样,注意这段代码非常重复!稍后我们将讲解如何改进它。